<?php

class PollController extends ControllerBase
{
	public function indexAction() {
		$this->view->setRenderLevel(Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
		$current = $this->request->getQuery('current', 'string');
		$news = News::find(array("created > '" . date('Y-m-d H:i:s', strtotime($current)) . "'", 'order' => 'created ASC'));
		//$news = News::find("id < 3");
		
		if (count($news) > 0) {
			$updated = array();
			foreach ($news as $new) {
				array_push($updated, array(
					'id' => urlencode($new->id),
					'title' => urlencode($new->title),
					'description_link' => urlencode($new->description_link),
					'description_text' => urlencode($new->description_text),
					'pubic_date' => $new->public_date,
					'link' => urlencode($new->link),
				));
			}
			$lastest = $news->getLast();
			echo json_encode(array('current' => date('YmdHis', strtotime($lastest->created)), 'message' => $updated));
			return;
		}
		
		usleep(10000000);
		clearstatcache();
		
		echo json_encode(array('current' => '', 'message' => array()));
	}
}